<?php

namespace app\shop\controller;

use think\Controller;

// 客户控制器
class Business extends Controller
{
    protected $BusinessModel = null;
    protected $SourceModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->SourceModel = model('Business.Source');
    }

    public function register()
    {
        if ($this->request->isAjax()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            // 获取客户来源
            $sourceid = $this->SourceModel->where(['name' => ['LIKE', "%家居商城%"]])->value('id');

            // 生成密码盐
            $salt = randstr();

            // 密码进行加密
            $password = md5($password . $salt);

            // 组装数据
            $data = [
                'mobile' => $mobile,
                'nickname' => build_encrypt($mobile, 3, 4), //脱敏显示
                'password' => $password,
                'salt' => $salt,
                'gender' => '0',
                'sourceid' => $sourceid,
                'deal' => '0',
                'money' => '0',
                'auth' => '0',
            ];

            // 执行插入语句,插入成功会自动返回影响函数
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            if ($result === FALSE) {
                //插入失败 让模型来返回错误信息
                $this->error($this->BusinessModel->getError());
                exit;
            } else {
                $this->success('注册成功', '/business/login');
                exit;
            }

            // var_dump($mobile, $password);
            // exit;
        }
    }

    public function login()
    {
        if ($this->request->isAjax()) {
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            //根据手机号码查询用户是否存在
            $business = $this->BusinessModel->where(['mobile' => $mobile])->find();

            if (!$business) {
                $this->error('该手机号用户不存在');
                exit;
            }

            if (empty($password)) {
                $this->error('密码不能为空');
                exit;
            }

            //验证密码
            $salt = $business['salt'];
            $repass = md5($password . $salt);

            //如果加密的密码和数据库的密码要是不一致就说明密码错误
            if ($repass != $business['password']) {
                $this->error('密码错误');
                exit;
            }

            unset($business['salt']);
            unset($business['password']);

            $this->success('登录成功', '/business/index', $business);
            exit;
        }
    }

    public function check()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');

            if (!$id || empty($mobile)) {
                $this->error('请先登录', '/business/login');
                exit;
            }

            //根据id和手机号 找出具体人
            $where = ['id' => $id, 'mobile' => $mobile];
            $business = $this->BusinessModel->where($where)->find();

            if (!$business) {
                $this->error('非法登录', '/business/login');
                exit;
            }

            unset($business['salt']);
            unset($business['password']);

            $this->success('用户存在', null, $business);
            exit;
        }
    }

    public function profile()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->param('id', 0, 'trim');
            $mobile = $this->request->param('mobile', '', 'trim');
            $nickname = $this->request->param('nickname', '', 'trim');
            $password = $this->request->param('password', '', 'trim');
            $gender = $this->request->param('gender', '0', 'trim');
            $email = $this->request->param('email', '', 'trim');
            $code = $this->request->param('code', '', 'trim');

            $business = $this->BusinessModel->find($id);

            if (!$business) {
                $this->error('用户不存在');
                exit;
            }

            //组装数据
            $data = [
                'id' => $id,
                'nickname' => $nickname,
                'mobile' => $mobile,
                'email' => $email,
                'gender' => $gender,
            ];

            //判断密码
            if (!empty($password)) {
                //重新生成密码盐
                $salt = randstr(10);
                $repass = md5($password . $salt);
                $data['salt'] = $salt;
                $data['password'] = $repass;
            }

            //邮箱 判断认证状态
            if ($business['email'] != $email) {
                $data['auth'] = '0';
            }

            //判断地区
            if (!empty($code)) {
                $parent = model('Region')->where(['code' => $code])->value('parentpath');

                //将字符串转换为数组
                $list = explode(',', $parent);

                $data['province'] = isset($list[0]) ? $list[0] : null;
                $data['city'] = isset($list[1]) ? $list[1] : null;
                $data['district'] = isset($list[2]) ? $list[2] : null;
            }

            //判断是否有上传图片
            if (isset($_FILES['avatar']) && $_FILES['avatar']['error'] == 0) {
                $success = build_upload('avatar');
                if ($success['code']) {
                    $data['avatar'] = $success['data'];
                } else {
                    $this->error($success['msg']);
                    exit;
                }
            }

            //更新
            $result = $this->BusinessModel->validate('common/Business/Business.profile')->isUpdate(true)->save($data);

            if ($result === FALSE) {
                $this->error($this->BusinessModel->getError());
                exit;
            }

            //更新成功，判断是否有旧图片 如果有就删除
            if (isset($data['avatar'])) {
                //判断他是否是一个图片，如果是就删除
                @is_file("." . $business['avatar']) && @unlink("." . $business['avatar']);
            }

            $this->success('更新成功', '/business/index');
            exit;
        }
    }
}
